package org.babygo.file;

import org.babygo.file.handler.FileMsgHandler;
import org.babygo.file.protocol.FilePackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.PriorityBlockingQueue;

public class FilePool {
    private static Logger logger = LoggerFactory.getLogger(FileMsgHandler.class);
    private static final Map<Short, File> fileMap = new HashMap<Short, File>();
    private static final Map<Short, RandomAccessFile> randomAccessFileMap = new HashMap<Short, RandomAccessFile>();
    private static final Map<Short, Long> fileWriteLengthMap = new HashMap<Short, Long>();
    private static final Map<Short, Integer> currentDownLoadPackageNumMap = new HashMap<Short, Integer>();
    private static final Map<Short, FilePackage[]> fileDataListMap = new HashMap<>();

    public static void putNewFile(Short id, File file) {
        if (fileMap.get(id) == null) {
            fileMap.put(id, file);
            currentDownLoadPackageNumMap.put(id, 0);
        }
    }

    public static void saveFileData(short id, int listSize, int index, FilePackage filePackage){
        FilePackage[] packageArr = fileDataListMap.get(id);
        if (packageArr == null){
            packageArr = initList(id, listSize);
        }
        try{
            packageArr[index] = filePackage;
        } catch (Exception e){
            logger.error("set list error!", e);
        }
    }

    private synchronized static FilePackage[] initList(short id ,int listSize){
        if (fileDataListMap.get(id) == null){
            FilePackage[] packageArr = new FilePackage[listSize];
            fileDataListMap.put(id, packageArr);
            return packageArr;
        } else {
            return fileDataListMap.get(id);
        }
    }

    public static FilePackage getFilePackage(short id, int index){
        return fileDataListMap.get(id)[index];
    }

    public static File getFile(Short id) {
        return fileMap.get(id);
    }

    public static void putNewRandomAccessFile(Short id, RandomAccessFile file) {
        randomAccessFileMap.put(id, file);
    }

    public static RandomAccessFile getRandomAccessFile(Short id) {
        return randomAccessFileMap.get(id);
    }

    public static void updateFileWriteLength(Short id, long length) {
        long len = getFileLength(id);
        fileWriteLengthMap.put(id, len + length);
    }

    public static void fileWriteLength(Short id, long length) {
        long len = getFileLength(id);
        fileWriteLengthMap.put(id, len + length);
    }

    public static Long getFileLength(Short id) {
        Long len = fileWriteLengthMap.get(id);
        if (len == null) {
            return 0L;
        }
        return len;
    }

    public static int getCurrentDownLoadPackageNum(Short id) {
        return currentDownLoadPackageNumMap.get(id);
    }

    public static void increseCurrentDownLoadPackageNum(Short id) {
        int num = getCurrentDownLoadPackageNum(id);
        currentDownLoadPackageNumMap.put(id, num + 1);
    }

    public static void closeFile(short id) {
        fileMap.remove(id);
        try {
            randomAccessFileMap.get(id).close();
        } catch (IOException e) {
            logger.error("关闭文件流IO错误!", e);
        }
        randomAccessFileMap.remove(id);
        fileWriteLengthMap.remove(id);
        currentDownLoadPackageNumMap.remove(id);
        fileDataListMap.remove(id);
    }

    public static void cleanPackageData(short id, int index){
        fileDataListMap.get(id)[index] = null;
    }
}